//
//  FWCustomTextView.m
//  BirdLoveSheep
//
//  Created by wwj on 16/4/14.
//  Copyright © 2016年 wwj. All rights reserved.
//

#import "FWCustomTextView.h"

@interface FWCustomTextView ()
@property (weak, nonatomic) UILabel *phLabel;
@end

@implementation FWCustomTextView

- (instancetype)initWithCoder:(NSCoder *)coder{
    self = [super initWithCoder:coder];
    if (self) {
        [self initView];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self initView];
    }
    return self;
}

- (void)initView{
    UILabel *phLabel = [[UILabel alloc] init];
    phLabel.textColor = [UIColor lightGrayColor];
    phLabel.font = [UIFont systemFontOfSize:15];
    phLabel.numberOfLines = 0;
    phLabel.x = 4;
    phLabel.y = 6;
    
    [self addSubview:phLabel];
    _phLabel = phLabel;
    
    self.alwaysBounceVertical = YES;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:nil];
}

- (void)setPlaceholder:(NSString *)placeholder{
    _placeholder = [placeholder copy];
    self.phLabel.text = _placeholder;
    [self layoutIfNeeded];
}

- (void)setPlaceholderColor:(UIColor *)placeholderColor{
    _placeholderColor = placeholderColor;
    self.phLabel.textColor = _placeholderColor;
    [self layoutIfNeeded];
}

- (void)setFont:(UIFont *)font{
    [super setFont:font];
    _phLabel.font = font;
    [self layoutIfNeeded];
}

- (void)setText:(NSString *)text{
    [super setText:text];
    [self textDidChange];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.phLabel.width = self.width - 2 * self.phLabel.x;
    [self.phLabel sizeToFit];
    MyLog(@"%f", self.width);
}

- (void)textDidChange{
    self.phLabel.hidden = [self hasText];
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
